package servlet;

import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 石小俊
 */
@WebServlet("*.session")
public class SessionServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path  = request.getServletPath();
        switch (path){
            case "/add.session":
                addSession(request,response);
                break;
            case "/remove.session":
                remove(request,response);
                break;
            case "/clear.session":
                clear(request,response);
                break;
            case "/count.session":
                count(request,response);
                break;
            default:
                response.setContentType("text/html;charset=utf-8");
                PrintWriter out = response.getWriter();
                out.println("<h1 style='color:red;'>对不起,您访问的资源不存在,请重新访问</h1>");
                break;
        }
    }

    public void addSession(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取session对象
        HttpSession session = request.getSession();
        // 添加session,本质上是向session作用域中存储数据
        session.setAttribute("username","admin");
        session.setAttribute("password","123456");
        User user = new User(1,"admin","123456","13812345678","江苏-南京");
        session.setAttribute("user",user);
        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
        // 当key相同时,会覆盖
    }

    public void remove(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("username","admin");
        // 设置生存时间,此时的值如果设置为0或者负数,并不是删除
        // 而是表示会话期间永不过期
//        session.setMaxInactiveInterval(0);

        // 真正的删除有两种方式
        // 1.删除指定key的数据
        session.removeAttribute("username");

        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
    }

    public void clear(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        // 2.清空session
        // 该方式本质上与删除无关
        // 首先删除当前session对象
        // 然后创建一个新的session对象
        // 由于之前的数据全部存储在之前的session中
        // 而新生成的session中尚未存储数据
        session.invalidate();
        request.getRequestDispatcher("/jsp/session.jsp").forward(request,response);
    }

    protected void count(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer count = (Integer) request.getSession().getAttribute("count");
        if(count == null){
            count = 1;
        } else{
            count++;
        }
        request.getSession().setAttribute("count",count);
        request.getRequestDispatcher("/jsp/count.jsp").forward(request,response);
    }

}
